﻿using Enterprises.Wpf.Sample.Data.Model;
using HandyControl.Tools;
using Newtonsoft.Json;
using System.IO;
using System.Windows;

namespace Enterprises.Wpf.Sample.Services
{
    /// <summary>
    /// 测试数据，菜单数据等
    /// </summary>
    public class DataService
    {
        public List<DemoDataModel> GetDemoDataList()
        {
            var list = new List<DemoDataModel>();
            for (var i = 1; i <= 20; i++)
            {
                var dataList = new List<DemoDataModel>();
                for (var j = 0; j < 3; j++)
                {
                    dataList.Add(new DemoDataModel
                    {
                        Index = j,
                        IsSelected = j % 2 == 0,
                        Name = $"SubName{j}",
                        Type = (DemoType)j
                    });
                }
                var model = new DemoDataModel
                {
                    Index = i,
                    IsSelected = i % 2 == 0,
                    Name = $"Name{i}",
                    Type = (DemoType)(i % 6 + 1),
                    DataList = dataList,
                    ImgPath = $"/HandyControlDemo;component/Resources/Img/Avatar/avatar{i % 6 + 1}.png",
                    Remark = new string(i.ToString()[0], 10)
                };
                list.Add(model);
            }

            return list;
        }

        public List<DemoInfoModel> GetLeftMenus(string groupName)
        {
            var infoList = new List<DemoInfoModel>();

            var stream = Application.GetResourceStream(new Uri($"Resources/Data/{groupName}.json", UriKind.Relative))?.Stream;
            if (stream == null) return infoList;

            string jsonStr;
            using (var reader = new StreamReader(stream))
            {
                jsonStr = reader.ReadToEnd();
            }

            var jsonObj = JsonConvert.DeserializeObject<dynamic>(jsonStr);
            foreach (var item in jsonObj)
            {
                var titleKey = (string)item.title;
                var title = titleKey;
                List<DemoItemModel> list = Convert2DemoItemList(item.demoItemList);

                var demoInfoModel = new DemoInfoModel
                {
                    Key = titleKey,
                    Title = title,
                    DemoItemList = list,
                    SelectedIndex = (int)item.selectedIndex,
                    IsGroupEnabled = (bool)item.group
                };

                infoList.Add(demoInfoModel);
            }

            return infoList;
        }

        private List<DemoItemModel> Convert2DemoItemList(dynamic list)
        {
            var resultList = new List<DemoItemModel>();

            foreach (var item in list)
            {
                var name = (string)item[0];
                string targetCtlName = item[1];
                string imageBrushName = item[2];
                var isNew = !string.IsNullOrEmpty((string)item[3]);
                var groupName = (string)item[4];
                if (string.IsNullOrEmpty(groupName))
                {
                    groupName = "Misc";
                }

                resultList.Add(new DemoItemModel
                {
                    Name = name,
                    TargetCtlName = targetCtlName,
                    ImageBrush = ResourceHelper.GetResource<object>(imageBrushName),
                    IsNew = isNew,
                    GroupName = groupName
                });
            }

            return resultList;
        }

    }
}
